package com.ruoyi.health.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.health.domain.Concern;
import com.ruoyi.health.domain.vo.ConcernVo;
import com.ruoyi.health.mapper.ConcernMapper;
import com.ruoyi.health.service.IConcernService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConcernServiceImpl implements IConcernService {


    @Autowired
    private ConcernMapper concernMapper;

    @Override
    public int insertConcern(Concern concern) {
        //不让重复关注同一个资源
        Long concernId = concernMapper.selectConcernIdByUserIdAndResourceId(concern);
        if(StringUtils.isNotNull(concernId)){
            throw new ServiceException("该资源已关注，不允许重复操作！");
        }
        //新增关注
        return concernMapper.insertConcern(concern);
    }

    @Override
    public int deleteConcern(Concern concern) {
        //只能取消自己所关注的养老资源
        //先查询用户关注养老资源的concern表Id
        Long concernId = concernMapper.selectConcernIdByUserIdAndResourceId(concern);
        if(StringUtils.isNotNull(concernId)){
            return concernMapper.deleteConcern(concernId);
        }
        throw new ServiceException("取消关注失败！");
    }

    @Override
    public List<ConcernVo> selectConcernWithResourceByUserId(Long userId) {
        List<ConcernVo> concernVoList = concernMapper.selectConcernWithResourceByUserId(userId);
        return concernVoList;
    }

    @Override
    public int getConcernStatus(Concern concern) {
        Long concernId = concernMapper.selectConcernIdByUserIdAndResourceId(concern);
        if(StringUtils.isNotNull(concernId)){
            return 1;  //已关注
        }
        return 0;   //未关注
    }


}
